import api, { getIMBaseUrl } from '../index'
import type { ResponseData,IMResponseData } from '../index'
import type {GoodsItem} from './goods'


export interface TiocomConfig {
	session_cookie_name:string//cookie键值
	res_server:string//资源地址
}
// 获取服务器的IM配置
export const getIMConfig = () =>
	api.get<IMResponseData<TiocomConfig>>('mytio/config/viewmodel.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true
		
	}
)
// 获取IM的接口地址
export const getIMUrlConfig = () =>
	api.get<IMResponseData<{
		ip:string,
		port:number,
		ssl:number
		timeout:string
	}>>('mytio/im/imserver.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true,
	}
)

// IM登录
export const postIMLogin = (data:{
	loginname:string
	nick:string
	pd5:string
	sign:string
}) =>
	api.post<{
		data:string
	}>('mytio/login/inner.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true,
		data,
	}
)
// IM退出登录
export const postIMOutLogin = () =>
	api.post('mytio/logout.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true,
		header: {
			"Content-Type": "application/x-www-form-urlencoded",
		},
	}
)
export interface IMUserInfo {
	id:number
	nick:string
}
// IM用户信息
export const getIMUser = () =>
	api.get<IMResponseData<IMUserInfo>>('mytio/user/curr.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true,
		custom:{
			goLogin:true
		}
	}
)


// 根据店铺id换取店铺的userThirdId
export const getStoreUserThirdId = (data:{userThirdId:string}) =>
	api.post<IMResponseData<{
		chatlinkid:string,
		chat:{
			bizid:string
			name:string
		}
	}>>('mytio/friend/addMerchant.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true,
		data,
		custom:{
			goLogin:true
		}
	}
)

export const uploadIMFilePath = async (filePath: string,data:{chatlinkid:string}) => {
	return api.upload<IMResponseData>('mytio/chat/img.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true,
		filePath,
		name: 'uploadFile',
		formData:data,
		custom: {
			isLoading: true,
			goLogin:true
		}
	})

}

export interface HistoryChatMsg {
	atnotreadcount: number
	atreadflag: number
	avatar:string
	bizid:string
	bizrole:number
	chatlinkid:string
	chatmode: number
	chatuptime: string
	focusflag: number
	fromnick: string
	id: string
	joinnum: 2
	lastmsgid: string
	lastmsguid:number
	linkflag: number
	linkid:string
	msgfreeflag:number
	msgresume: string
	msgtype: number
	name: string
	notreadcount: number
	notreadstartmsgid:string
	opernick: ""
	readflag: number
	sendtime: string
	status: number
	sysflag: number
	sysmsgkey: ""
	tonicks: ""
	topflag: number
	toreadflag:number
	uid: number
	viewflag: number
}


// IM聊天列表
export const getIMChatList = () =>
	api.get<IMResponseData<HistoryChatMsg[]>>('mytio/chat/list.tio_x', {
		baseURL:getIMBaseUrl(),
		withCredentials:true,
		custom: {
			goLogin:true
		}
	}
)


export type IMStoreInfo = {
	id:string
	nickName:string
	photo:string
}
// 批量获取店铺头像、名称
export const getIMStoreListInfo = (data: { ids: string }) =>
	api.get<ResponseData<IMStoreInfo[]>>('merchant/customer/store/get/photo/batch', {
		data
	}
)


export type IMOrderItem = {
	commodityCount:number//商品数量
	deputyMoney:number//商品单价
	deputyMoneyPay:number//支付金额
	commodityName:string//商品名称
	commoditySpecId:string//规格id
	commoditySpecName:string//规格名称
	commodityUrl:string//商品图
	deputyId:string
	id:number
	orderId:string
	createTime:string
}

// IM专用订单列表，每个订单都是一个商品
export const getIMOrderList = (data: { deputyId?: string,storeId:string }) =>
	api.get<ResponseData<{
		rows:IMOrderItem[]
	}>>('order/api/main/deputy/list', {
		data
	}
)
// IM专用商品列表，查询 1预览商品 2 收藏商品 3 购物车商品 4 已购商品
export const getIMFootmarkGoodsList = (data:{storeId:string}) =>
	api.get<ResponseData<GoodsItem[]>>('/customer/api/product/list/1', {
		data
	}
)
export const getIMCollectGoodsList = (data:{storeId:string}) =>
	api.get<ResponseData<GoodsItem[]>>('/customer/api/product/list/2', {
		data
	}
)
export const getIMCartGoodsList = (data:{storeId:string}) =>
	api.get<ResponseData<GoodsItem[]>>('/customer/api/product/list/3', {
		data
	}
)
export const getIMBuyGoodsList = (data:{storeId:string}) =>
	api.get<ResponseData<GoodsItem[]>>('/customer/api/product/list/4', {
		data
	}
)
